package com.order.orderback.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.order.orderback.entity.*;
import com.order.orderback.mapper.*;
import com.order.orderback.service.StoreService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class StoreServiceImpl implements StoreService {

    @Autowired
    private StoresMapper storesMapper;
    @Autowired
    private StoreTagsMapper storeTagsMapper;
    @Autowired
    private MenuCategoriesMapper menuCategoriesMapper;
    @Autowired
    private MenuItemsMapper menuItemsMapper;
    @Autowired
    private SwiperImagesMapper swiperImagesMapper;

    /**
     * 获取当前地区的商家信息
     * @param params
     * @return
     */
    @Override
    public List<Map<String, Object>> getShops(Map<String, Object> params) {
        QueryWrapper<Stores> queryWrapper = new QueryWrapper<>();
        if (params != null) {
            for (Map.Entry<String, Object> entry : params.entrySet()) {
                queryWrapper.eq(entry.getKey(), entry.getValue());
            }
        }
        List<Stores> stores = storesMapper.selectList(queryWrapper);
        List<Map<String, Object>> result = new ArrayList<>();
        for (Stores store : stores) {
            Map<String, Object> storeMap = new HashMap<>();
            storeMap.put("id", store.getId());
            storeMap.put("name", store.getName());
            storeMap.put("logo", store.getLogo());
            storeMap.put("rating", store.getRating());
            storeMap.put("monthlySales", store.getMonthlySales());
            storeMap.put("minPrice", store.getMinPrice());

            QueryWrapper<StoreTags> tagWrapper = new QueryWrapper<>();
            tagWrapper.eq("store_id", store.getId());
            List<StoreTags> tags = storeTagsMapper.selectList(tagWrapper);
            List<String> tagNames = new ArrayList<>();
            for (StoreTags tag : tags) {
                tagNames.add(tag.getTagName());
            }
            storeMap.put("tags", tagNames);
            result.add(storeMap);
        }
        return result;
    }

    /**
     * 获取商家详细信息
     * @param id
     * @return
     */
    @Override
    public Map<String, Object> getShopDetail(Integer id) {
        Stores store = storesMapper.selectById(id);
        if (store == null) {
            return null;
        }
        Map<String, Object> result = new HashMap<>();
        result.put("id", store.getId());
        result.put("name", store.getName());
        result.put("logo", store.getLogo());
        result.put("rating", store.getRating());
        result.put("monthlySales", store.getMonthlySales());
        result.put("minPrice", store.getMinPrice());

        QueryWrapper<StoreTags> tagWrapper = new QueryWrapper<>();
        tagWrapper.eq("store_id", store.getId());
        List<StoreTags> tags = storeTagsMapper.selectList(tagWrapper);
        List<String> tagNames = new ArrayList<>();
        for (StoreTags tag : tags) {
            tagNames.add(tag.getTagName());
        }
        result.put("tags", tagNames);

        QueryWrapper<MenuCategories> categoryWrapper = new QueryWrapper<>();
        categoryWrapper.eq("store_id", store.getId());
        List<MenuCategories> categories = menuCategoriesMapper.selectList(categoryWrapper);
        List<Map<String, Object>> menu = new ArrayList<>();
        for (MenuCategories category : categories) {
            Map<String, Object> categoryMap = new HashMap<>();
            categoryMap.put("id", category.getId());
            categoryMap.put("name", category.getName());

            QueryWrapper<MenuItems> itemWrapper = new QueryWrapper<>();
            itemWrapper.eq("category_id", category.getId());
            List<MenuItems> items = menuItemsMapper.selectList(itemWrapper);
            List<Map<String, Object>> itemList = new ArrayList<>();
            for (MenuItems item : items) {
                Map<String, Object> itemMap = new HashMap<>();
                itemMap.put("id", item.getId());
                itemMap.put("name", item.getName());
                itemMap.put("price", item.getPrice());
                itemMap.put("description", item.getDescription());
                itemList.add(itemMap);
            }
            categoryMap.put("items", itemList);
            menu.add(categoryMap);
        }
        result.put("menu", menu);
        return result;
    }

    /**
     * 获取轮播图
     * @return
     */
    @Override
    public List<Map<String, Object>> getSwiperImages() {
        // 这里可以根据实际情况从数据库中查询轮播图信息
        QueryWrapper<SwiperImages> queryWrapper = new QueryWrapper<>();

        List<SwiperImages> swiperImagesList = swiperImagesMapper.selectList(queryWrapper);

        List<Map<String, Object>> result = new ArrayList<>();
        int index = 0;
        for (SwiperImages swiperImage : swiperImagesList) {
            if (index == 3){
                return result;
            }
            Map<String, Object> map = new HashMap<>();
            map.put("image", swiperImage.getImageUrl());
            result.add(map);
            index++;
        }
        return result;
    }

}
